﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace A.HTMlServer.HTML
{
    /// <summary>
    /// 获得一个页面
    /// </summary>
    public class GetOnePage
    {
        private HtmlPart hp = new HtmlPart();
        public int J { set; get; }
        public int wrong { set; get; }
        public int Sum { set; get;}
        public GetOnePage() { }


        /// <summary>
        /// 美容养颜
        /// </summary>
        /// <param name="Url"></param>
        public void GetBeautifulPageHtml(string Url)
        {
            string Html=hp.GetHtml(Url);
            if (Html == null)
            {
                wrong++;
                return;
            }

            A.HTMlServer.ArticleModel model = new A.HTMlServer.ArticleModel();

            model.Title = hp.ToTitle(Html, "<h1>(.*?)</h1>", 1);
            model.Content = hp.ToContent(Html, "<div class=\"article\">(.*?)</div>\\s+<div class=\"page\">", 1);

            model.page=1;
            ///分页内容
            List<string> list = hp.GetCountPage(Html, "<div class=\"page\">(.*?)</div>", 1,"href=\"(\\d+_\\d+).html\"",1);
            if(list.Count>0)
            {
                model.page+=list.Count;
                list.ForEach(
                    str => 
                    {
                            model.Content +="<!--page-->"+hp.GetPageContent(Url.Substring(0,Url.LastIndexOf('/')+1) + str + ".html", "<div class=\"article\">(.*?)</div>\\s+<div class=\"page\">", 1);
                    }
                );

            }

            model.Content = hp.RemovePart(model.Content, "<p align=\"?right\"?>\\s*（责任编辑：.*?）\\s*(</p>)?","");

            //model.Content = hp.RemovePart(model.Content, "<div\\s+?(id=\"hzh_beauty\" class=\"hzh\")|(class=\"hzh\" id=\"hzh_beauty\")\\s*?>\\s*?<iframe.*?</iframe>\\s*?</div>", "");

            model.Content = hp.RemovePart(model.Content, "<div.*?id=\"?hzh_beauty\"?.*?>\\s*?<iframe.*?</iframe>\\s*?</div>", "");


            model.Introduction = hp.ToIntroduction(Html, "<meta name=\"Description\" content=\"(.*?)\">", 1);

            model.Date = DateTime.Now;
            model.From = "39健康网";

            model.Code = "39";

            //model.CodeName = hp.ToCodeName(Html, "<div class=\"face\"><a.*?>(.*?)</a></div>", 1);

            //&gt; <a class="ClassNav" href="http://face.39.net/mrxf/fxdiy/">发型DIY</a>  > 正文</p>
            //<a href="http://face.39.net/mrxf/fxdiy/" class="ClassNav">发型DIY</a>
            //<a class="ClassNav" href="http://face.39.net/mrxf/fxdiy/">发型DIY</a>> 正文
            model.CodeName = hp.ToCodeName(Html, "<a class=\"ClassNav\" href=\"http://face.39.net/\\S+?/\\S+?/\\S*?\">([\u4e00-\u9fa5]+)</a>\\s+?>\\s+?正文", 1);

            A.HTMlServer.ArticleData.DatatoSqlArticle(model);

            J++;
            
        }

        /// <summary>
        /// Hii girl
        /// </summary>
        /// <param name="Url"></param>
        public void GetHiiGrilPageHtml(string Url)
        {
            string Html = hp.GetHtml(Url);
            if (Html == null|| Html.Contains("对不起，你输入的参数有误！"))
            {
                wrong++;
                return;
            }

            A.HTMlServer.ArticleModel model = new A.HTMlServer.ArticleModel();

            model.Title = hp.ToTitle(Html, "<h2>(.*?)</h2>", 1);
            var RContent= "<div class=\"content\">(.*?)</div><!-- /content -->";
            model.Content = hp.ToContent(Html,RContent, 1);

            model.page = 1;

            //分页内容
            //view.php?aid=20&pageno=2
            List<string> list = hp.GetCountPage(Html, "<ul class=\"pagelist\">(.*?)</ul>", 1, "href='(view.php\\?.*?)'", 1);
            if (list.Count > 0)
            {
                model.page += list.Count;
                list.ForEach(
                    str =>
                    {
                        model.Content += "<!--page-->" + hp.GetPageContent(Url.Substring(0, Url.LastIndexOf('/') + 1) + str, RContent, 1);
                    }
                );

            }

            //model.Content = hp.RemovePart(model.Content, "<p align=\"?right\"?>\\s*（责任编辑：.*?）\\s*(</p>)?", "");

            //model.Content = hp.RemovePart(model.Content, "<div\\s+?(id=\"hzh_beauty\" class=\"hzh\")|(class=\"hzh\" id=\"hzh_beauty\")\\s*?>\\s*?<iframe.*?</iframe>\\s*?</div>", "");

            //model.Content = hp.RemovePart(model.Content, "<div.*?id=\"?hzh_beauty\"?.*?>\\s*?<iframe.*?</iframe>\\s*?</div>", "");
            

            model.Introduction = hp.ToIntroduction(Html, "<meta name=\"description\" content=\"(.*?)\"\\s*/>", 1);

            model.Date = DateTime.Now;
            model.From = "追女孩";

            model.Code = "30";

            model.CodeName = hp.ToCodeName(Html, "<div class=\"place\">(.*?)< a href='.*?'>([\u4e00-\u9fa5]*)</a> >", 1);

            A.HTMlServer.ArticleData.DatatoSqlArticle(model);

            J++;

        }
    }

}
